package com.muyu.payment.controller;

import java.util.List;

import com.muyu.common.core.web.controller.BaseController;
import com.muyu.common.core.domain.Result;
import com.muyu.common.core.web.page.PageQueryModel;
import com.muyu.common.core.utils.poi.ExcelUtil;
import com.muyu.payment.domain.PaymentIndent;
import com.muyu.payment.domain.model.PaymentIndentQueryModel;
import com.muyu.payment.domain.model.PaymentIndentPageQueryModel;
import com.muyu.payment.domain.model.PaymentIndentSaveModel;
import com.muyu.payment.domain.model.PaymentIndentEditModel;
import com.muyu.payment.domain.req.PaymentCallbackReq;
import com.muyu.payment.domain.resp.PaymentCallbackResp;
import com.muyu.payment.service.PaymentIndentService;
import com.muyu.payment.service.PaymentDataSyncService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;

/**
 * 支付订单Controller
 *
 * @author muyu
 * @date 2025-09-18
 */
@Tag(name = "支付订单管理", description = "支付订单相关接口")
@RestController
@RequestMapping("/indent")
public class PaymentIndentController extends BaseController {
    
    @Autowired
    private PaymentIndentService paymentIndentService;
    
    @Autowired
    private PaymentDataSyncService paymentDataSyncService;

    /**
     * 查询支付订单列表
     */
    @Operation(summary = "查询支付订单列表", description = "分页查询支付订单列表")
    @GetMapping("/list")
    public Result<PageQueryModel<PaymentIndentPageQueryModel>> list(PaymentIndentQueryModel queryModel) {
        PageQueryModel<PaymentIndentPageQueryModel> pageQueryModel = paymentIndentService.pageQuery(queryModel);
        return Result.success(pageQueryModel);
    }

    /**
     * 导出支付订单列表
     */
    @Operation(summary = "导出支付订单列表", description = "导出支付订单数据到Excel")
    @PostMapping("/export")
    public void export(HttpServletResponse response, PaymentIndent queryModel) {
        List<PaymentIndent> list = paymentIndentService.exportList(queryModel);
        ExcelUtil<PaymentIndent> util = new ExcelUtil<PaymentIndent>(PaymentIndent.class);
        util.exportExcel(response, list, "支付订单数据");
    }

    /**
     * 获取支付订单详细信息
     */
    @Operation(summary = "获取支付订单详情", description = "根据ID获取支付订单详细信息")
    @GetMapping(value = "/{id}")
    public Result<PaymentIndent> getInfo(@PathVariable("id") Long id) {
        return success(paymentIndentService.getById(id));
    }

    /**
     * 新增支付订单
     */
    @Operation(summary = "新增支付订单", description = "创建新的支付订单记录")
    @PostMapping
    public Result<String> add(@RequestBody PaymentIndentSaveModel saveModel) {
        paymentIndentService.save(saveModel);
        return Result.success("订单创建成功");
    }

    /**
     * 修改支付订单
     */
    @Operation(summary = "修改支付订单", description = "修改支付订单信息")
    @PutMapping
    public Result<String> edit(@RequestBody PaymentIndentEditModel editModel) {
        paymentIndentService.edit(editModel);
        return Result.success();
    }

    /**
     * 删除支付订单
     */
    @Operation(summary = "删除支付订单", description = "删除支付订单记录")
    @DeleteMapping("/{ids}")
    public Result<String> remove(@PathVariable Long[] ids) {
        return toAjax(paymentIndentService.removeByIds(List.of(ids)));
    }

    /**
     * 根据订单号查询支付订单
     */
    @Operation(summary = "根据订单号查询", description = "根据订单号查询支付订单")
    @GetMapping("/order/{orderNo}")
    public Result<PaymentIndent> getByOrderNo(@PathVariable("orderNo") String orderNo) {
        PaymentIndent paymentIndent = paymentIndentService.getByOrderNo(orderNo);
        return Result.success(paymentIndent);
    }

    /**
     * 更新订单状态
     */
    @Operation(summary = "更新订单状态", description = "更新支付订单状态")
    @PutMapping("/status/{orderNo}/{status}")
    public Result<String> updateStatus(@PathVariable("orderNo") String orderNo, @PathVariable("status") Integer status) {
        boolean success = paymentIndentService.updateOrderStatus(orderNo, status);
        return success ? Result.success("状态更新成功") : Result.error("状态更新失败");
    }

    /**
     * 支付成功回调
     */
    @Operation(summary = "支付成功回调", description = "处理第三方支付成功回调")
    @PostMapping("/callback")
    public Result<PaymentCallbackResp> paymentCallback(@RequestBody PaymentCallbackReq callbackReq) {
        PaymentCallbackResp response = paymentIndentService.handlePaymentCallback(callbackReq);
        return Result.success(response);
    }

    /**
     * 检查并修复数据一致性
     */
    @Operation(summary = "检查数据一致性", description = "检查并修复支付订单与支付记录的数据一致性")
    @PostMapping("/check-consistency/{orderNo}")
    public Result<String> checkDataConsistency(@PathVariable("orderNo") String orderNo) {
        boolean success = paymentDataSyncService.checkAndFixDataConsistency(orderNo);
        return success ? Result.success("数据一致性检查完成") : Result.error("数据一致性检查失败");
    }

    /**
     * 批量检查并修复数据一致性
     */
    @Operation(summary = "批量检查数据一致性", description = "批量检查并修复所有支付订单的数据一致性")
    @PostMapping("/batch-check-consistency")
    public Result<String> batchCheckDataConsistency() {
        int fixedCount = paymentDataSyncService.batchCheckAndFixDataConsistency();
        return Result.success("批量检查完成，修复订单数量：" + fixedCount);
    }
}